package com.powerunion.datacollection.workflow.fsm.core.task;

import org.apache.log4j.Logger;

import com.powerunion.datacollection.dao.impl.CascadeFieldDaoImpl;
import com.powerunion.datacollection.workflow.WFEngineException;
import com.powerunion.datacollection.workflow.fsm.FMName;
import com.powerunion.datacollection.workflow.fsm.WFTask;
import com.powerunion.datacollection.workflow.fsm.exception.AlreadyRunning;
import com.powerunion.datacollection.workflow.fsm.exception.AlreadySuspended;
import com.powerunion.datacollection.workflow.fsm.exception.CannotSuspend;
import com.powerunion.datacollection.workflow.fsm.exception.NotRunning;
import com.powerunion.datacollection.workflow.fsm.exception.NotSuspended;
import com.powerunion.datacollection.workflow.fsm.state.task.WFTaskAbortState;
import com.powerunion.datacollection.workflow.fsm.state.task.WFTaskCancelState;
import com.powerunion.datacollection.workflow.fsm.state.task.WFTaskCompleteState;
import com.powerunion.datacollection.workflow.fsm.state.task.WFTaskResumeState;
import com.powerunion.datacollection.workflow.fsm.state.task.WFTaskRunningState;
import com.powerunion.datacollection.workflow.fsm.state.task.WFTaskStartState;
import com.powerunion.datacollection.workflow.fsm.state.task.WFTaskSuspendState;
import com.powerunion.datacollection.workflow.fsm.state.task.WFTaskTerminateState;
import com.powerunion.datacollection.workflow.util.observer.AbleImp;
import com.powerunion.util.log.LogHelper;

public class WFTaskContext extends AbleImp{

	private static final Logger logger = LogHelper.getInstance().getLogger(
			WFTaskContext.class);
	
	public final static WFTask startState = new WFTaskStartState();
	public final static WFTask cancelState = new WFTaskCancelState();
	public final static WFTask completeState = new WFTaskCompleteState();
	public final static WFTask runningState = new WFTaskRunningState();
	public final static WFTask suspendState = new WFTaskSuspendState();
	public final static WFTask resumeState = new WFTaskResumeState();
	public final static WFTask terminateState = new WFTaskTerminateState();
	public final static WFTask abortState = new WFTaskAbortState();
	
	//当前状态
	private WFTask currentState;
 
	public WFTask getCurrentState() {
		return currentState;
	}

	public void setCurrentState(WFTask currentState) {
		this.currentState = currentState;
		this.currentState.setContext(this);
	}
	
	public void toCancel() {
		this.currentState.cancel();
	}
	public  void toRun() throws AlreadyRunning{
		if(this.currentState.getStateNum() != FMName.wk_task_state_num_notstart){
			throw new AlreadyRunning(message(FMName.wk_task_state_name_run));
		}
		this.updateToNotify(FMName.wk_task_event_to_run);
	}

	public void toSuspend() throws CannotSuspend,NotRunning,AlreadySuspended{
		if(this.currentState.getStateNum() == FMName.wk_task_state_num_suspend){
			throw new AlreadySuspended(message(FMName.wk_task_state_name_suspend));
		}
		if(this.currentState.getStateNum() != FMName.wk_task_state_num_run){
			throw new NotRunning(message(FMName.wk_task_state_name_run));
		}
		this.updateToNotify(FMName.wk_task_event_to_suspend);
	}
	public void toAbort() throws NotSuspended{
		if(this.currentState.getStateNum() != FMName.wk_task_state_num_suspend){
			throw new NotSuspended(message(FMName.wk_task_state_name_suspend));
		}
		this.currentState.abort();
	}
	public void toTerminate(){
		this.currentState.terminate();
	}
	public void toComplete(){
		this.currentState.complete();
	}

	
	private String message(String eventStr){
		String currStateStr = "当前状态为["+currentState.getStateName()+"]!";
		String messageErr =  "过程["+"id"+"] 执行 (事件："+eventStr+") 失败," + currStateStr;	
		return messageErr;
	}
	
	private void updateToNotify(int eventNum){
		try{
			if(eventNum == FMName.wk_task_event_to_run){
				this.currentState.run();
			}
			if(eventNum == FMName.wk_task_event_to_suspend){
				this.currentState.suspend();
			}
			if(eventNum == FMName.wk_task_event_to_complete){
				this.currentState.complete();
			}
			
			logger.debug("过程：[" + "id"+ "], 事件触发：[" + eventNum + "], 状态:["+ currentState.getStateName() + "]");
			this.updateState();
			this.updateLastTime();
			
		}catch(Exception e){
			e.printStackTrace();
		}
	}
	
	protected void updateState(){
		
	}
	
	protected void updateLastTime() {		
	
	}
	
}
